package com.sailorj.controller;

import com.sailorj.entity.SysUser;
import com.sailorj.service.SysUserService;
import com.sailorj.util.PageBean;
import com.sailorj.util.SysLoging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
* @Description: 用户信息控制层 
* @Param: 
* @return: 
* @Author: huangshengjun
* @Date: 2020/4/2 23:05
*/
//@ResponseBody  //如果想要return页面，就去掉这个全局返回body，加在具体的方法上面。@RestController也不能全局使用
@CrossOrigin  //后端解决跨域方法注解
@Controller
@RequestMapping("/user") //访问的路径
public class SysUserController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private SysUserService sysUserService;

//    @RequestMapping("/hello")
//    public String test(){
//        logger.info("hello word!!!!!");
//        return "hello word!!!!!";
//    }

//    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    public String login(SysUser user){
//        System.out.println("===login数据====" + GsonUtil.GsonString(user));
//        return sysUserService.login(user);
//    }

    @ResponseBody
    @SysLoging("登录")
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public SysUser login(SysUser user) {
        logger.info(user.getUserName()+"=="+user.getPassWord());
            return sysUserService.login(user);
    }


    /**
     * restful风格实现
     * @param user
     * @return
     */
    @ResponseBody
    @SysLoging("添加用户信息") //根据SysLoging的注解信息，进行方法的注释
    @RequestMapping(value = "/addUser",method = RequestMethod.POST) //restful风格
    public String addUser(SysUser user){
        user.setIsDelete(0);
        return sysUserService.addUser(user);
    }

    @ResponseBody
    @SysLoging("根据id查询用户信息")
    @RequestMapping(value = "/getUserById",method = RequestMethod.GET)
    public SysUser getUserById(int id){
        return sysUserService.getUserById(id);
    }

    @ResponseBody
    @SysLoging("获取用户信息列表")
    @RequestMapping(value = "/getUserList",method = RequestMethod.GET)
    PageBean getUserList(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size){
        return sysUserService.getUserList(page,size);
    }

    @ResponseBody
    @SysLoging("删除用户信息")
    @RequestMapping(value = "/deleteUser",method = RequestMethod.DELETE)
    public void deleteUser(int id) {
        SysUser user1 = sysUserService.getUserById(id);
        user1.setIsDelete(1);
        sysUserService.updateUserById(user1);
    }

    @ResponseBody
    @SysLoging("更新用户信息")
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public void updateUser(SysUser user) {
        user.setIsDelete(0);
        sysUserService.updateUserById(user);
    }

}
